package chat.user.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import chat.test.CommonUtils;
import chat.user.domain.User;
import chat.user.service.UserException;
import chat.user.service.UserService;

/**
 * Servlet implementation class UserEditServlet
 */
@WebServlet("/UserEditServlet")
public class UserEditServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	UserService userService = new UserService();
  

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
	
		
		String edit = request.getParameter("edit");
		String id = request.getParameter("id");
		
		User user = userService.findById(Integer.parseInt(id));
		request.setAttribute("user", user);
		request.setAttribute("edit", edit);
		request.getRequestDispatcher("/view/user/edit.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
		
		User user = new User();
		
		String id = request.getParameter("id");
		user.setId(Integer.parseInt(id));
		
		User session_user = (User)request.getSession().getAttribute("session_user");
		user.setUpdateby(session_user.getUsername());
		
		String username = request.getParameter("username");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String note = request.getParameter("note");
		String status = request.getParameter("status");
		String mode = request.getParameter("mode");
		String sb = null;
		String value = null;
		
		if(username!=null)
		{
			user.setUsername(username);
			sb="username";
			value=username;
		}
		if(sex!=null)
		{
			user.setSex(sex);
			sb="sex";
			value=sex;
		}
		
		if(birthday!=null)
		{
			user.setBirthday(birthday);
			sb="birthday";
			value=birthday;
		}
		
		if(email!=null)
		{
			user.setEmail(email);
			sb="email";
			value=email;
		}
		
		if(phone!=null)
		{
			user.setPhone(phone);
			sb="phone";
			value=phone;
		}
		
		if(note!=null)
		{
			user.setNote(note);
			sb="note";
			value=note;
		}
		if(status!=null)
		{
			user.setStatus(Integer.parseInt(status));
			sb="status";
			value=status;
		}
		if(mode!=null)
		{
			user.setMode(Integer.parseInt(mode));
			sb="mode";
			value=mode;
		}
		
		
		userService.edit(user,sb,value);
		
		Map<String,String> msgMap  = new HashMap<String,String>();
		msgMap.put("title", "编辑成功");
		msgMap.put("desc", "编辑成功,去个人信息看看吧.");
		msgMap.put("type", "个人信息");
		msgMap.put("url", "/view/user/desc.jsp");
		request.setAttribute("msg", msgMap);
		request.getRequestDispatcher("/view/msg.jsp").forward(request, response);
	}
}
